/* ===========================================================
 * TradeManager : a application to trade strategies for the Java(tm) platform
 * ===========================================================
 *
 * (C) Copyright 2011-2011, by Simon Allen and Contributors.
 *
 * Project Info:  org.trade
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA.
 *
 * [Java is a trademark or registered trademark of Oracle, Inc.
 * in the United States and other countries.]
 *
 * (C) Copyright 2011-2011, by Simon Allen and Contributors.
 *
 * Original Author:  Simon Allen;
 * Contributor(s):   -;
 *
 * Changes
 * -------
 *
 */
package org.trade.persistent.dao;

// Generated Feb 21, 2011 12:43:33 PM by Hibernate Tools 3.4.0.CR1

import static javax.persistence.GenerationType.IDENTITY;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.Version;

import org.trade.core.dao.Aspect;
import org.trade.core.valuetype.Money;

/**
 * Trade generated by hbm2java
 * @author Simon Allen
 * @version $Revision: 1.0 $
 */
@Entity
@Table(name = "trade")
public class Trade extends Aspect implements java.io.Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 715993951200025530L;
	private Tradestrategy tradestrategy;
	private TradestrategyId tradestrategyId;
	private BigDecimal averagePrice;
	private Boolean isOpen = new Boolean(false);
	private Integer openQuantity = new Integer(0);
	private String side;
	private BigDecimal profitLoss;
	private BigDecimal totalCommission;
	private Integer totalQuantity;
	private BigDecimal totalValue;
	private List<TradeOrder> tradeOrders = new ArrayList<TradeOrder>(0);

	public Trade() {
	}

	/**
	 * Constructor for Trade.
	 * @param tradestrategy Tradestrategy
	 * @param side String
	 */
	public Trade(Tradestrategy tradestrategy, String side) {
		this.tradestrategy = tradestrategy;
		this.side = side;
	}

	/**
	 * Constructor for Trade.
	 * @param tradestrategy Tradestrategy
	 * @param averagePrice BigDecimal
	 * @param openQuantity Integer
	 * @param profitLoss BigDecimal
	 * @param side String
	 * @param totalCommission BigDecimal
	 * @param totalQuantity Integer
	 * @param totalValue BigDecimal
	 * @param tradeOrders List<TradeOrder>
	 */
	public Trade(Tradestrategy tradestrategy, BigDecimal averagePrice,
			Integer openQuantity, BigDecimal profitLoss, String side,
			BigDecimal totalCommission, Integer totalQuantity,
			BigDecimal totalValue, List<TradeOrder> tradeOrders) {
		this.tradestrategy = tradestrategy;
		this.averagePrice = averagePrice;
		this.openQuantity = openQuantity;
		this.side = side;
		this.profitLoss = profitLoss;
		this.totalCommission = totalCommission;
		this.totalQuantity = totalQuantity;
		this.totalValue = totalValue;
		this.tradeOrders = tradeOrders;
	}

	/**
	 * Method getIdTrade.
	 * @return Integer
	 */
	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "idTrade", unique = true, nullable = false)
	public Integer getIdTrade() {
		return this.id;
	}

	/**
	 * Method setIdTrade.
	 * @param idTrade Integer
	 */
	public void setIdTrade(Integer idTrade) {
		this.id = idTrade;
	}

	/**
	 * Method getTradestrategy.
	 * @return Tradestrategy
	 */
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "idTradeStrategy", insertable = true, updatable = true, nullable = false)
	public Tradestrategy getTradestrategy() {
		return this.tradestrategy;
	}

	/**
	 * Method setTradestrategy.
	 * @param tradestrategy Tradestrategy
	 */
	public void setTradestrategy(Tradestrategy tradestrategy) {
		this.tradestrategy = tradestrategy;
	}

	/**
	 * Method getTradestrategyId.
	 * @return TradestrategyId
	 */
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "idTradeStrategy", insertable = false, updatable = false, nullable = false)
	public TradestrategyId getTradestrategyId() {
		return this.tradestrategyId;
	}

	/**
	 * Method setTradestrategyId.
	 * @param tradestrategyId TradestrategyId
	 */
	public void setTradestrategyId(TradestrategyId tradestrategyId) {
		this.tradestrategyId = tradestrategyId;
	}

	/**
	 * Method getAveragePrice.
	 * @return BigDecimal
	 */
	@Column(name = "averagePrice", precision = 10)
	public BigDecimal getAveragePrice() {
		return this.averagePrice;
	}

	/**
	 * Method setAveragePrice.
	 * @param averagePrice BigDecimal
	 */
	public void setAveragePrice(BigDecimal averagePrice) {
		this.averagePrice = averagePrice;
	}

	/**
	 * Method getIsOpen.
	 * @return Boolean
	 */
	@Column(name = "isOpen", nullable = false)
	public Boolean getIsOpen() {
		return this.isOpen;
	}

	/**
	 * Method setIsOpen.
	 * @param isOpen Boolean
	 */
	public void setIsOpen(Boolean isOpen) {
		this.isOpen = isOpen;
	}

	/**
	 * Method getOpenQuantity.
	 * @return Integer
	 */
	@Column(name = "openQuantity")
	public Integer getOpenQuantity() {
		return this.openQuantity;
	}

	/**
	 * Method setOpenQuantity.
	 * @param openQuantity Integer
	 */
	public void setOpenQuantity(Integer openQuantity) {
		this.openQuantity = openQuantity;
	}

	/**
	 * Method getProfitLoss.
	 * @return BigDecimal
	 */
	@Column(name = "profitLoss", precision = 10)
	public BigDecimal getProfitLoss() {
		return this.profitLoss;
	}

	/**
	 * Method setProfitLoss.
	 * @param profitLoss BigDecimal
	 */
	public void setProfitLoss(BigDecimal profitLoss) {
		this.profitLoss = profitLoss;
	}

	/**
	 * Method getSide.
	 * @return String
	 */
	@Column(name = "side", nullable = false, length = 3)
	public String getSide() {
		return this.side;
	}

	/**
	 * Method setSide.
	 * @param side String
	 */
	public void setSide(String side) {
		this.side = side;
	}

	/**
	 * Method getTotalCommission.
	 * @return BigDecimal
	 */
	@Column(name = "totalCommission", precision = 10)
	public BigDecimal getTotalCommission() {
		return this.totalCommission;
	}

	/**
	 * Method setTotalCommission.
	 * @param totalCommission BigDecimal
	 */
	public void setTotalCommission(BigDecimal totalCommission) {
		this.totalCommission = totalCommission;
	}

	/**
	 * Method getTotalQuantity.
	 * @return Integer
	 */
	@Column(name = "totalQuantity")
	public Integer getTotalQuantity() {
		return this.totalQuantity;
	}

	/**
	 * Method setTotalQuantity.
	 * @param totalQuantity Integer
	 */
	public void setTotalQuantity(Integer totalQuantity) {
		this.totalQuantity = totalQuantity;
	}

	/**
	 * Method getTotalValue.
	 * @return BigDecimal
	 */
	@Column(name = "totalValue", precision = 10)
	public BigDecimal getTotalValue() {
		return this.totalValue;
	}

	/**
	 * Method setTotalValue.
	 * @param totalValue BigDecimal
	 */
	public void setTotalValue(BigDecimal totalValue) {
		this.totalValue = totalValue;
	}

	/**
	 * Method getVersion.
	 * @return Integer
	 */
	@Version
	@Column(name = "version")
	public Integer getVersion() {
		return this.version;
	}

	/**
	 * Method setVersion.
	 * @param version Integer
	 */
	public void setVersion(Integer version) {
		this.version = version;
	}

	/**
	 * Method getTradeOrders.
	 * @return List<TradeOrder>
	 */
	@OneToMany(mappedBy = "trade", fetch = FetchType.LAZY, cascade = {
			CascadeType.REFRESH, CascadeType.REMOVE })
	public List<TradeOrder> getTradeOrders() {
		return this.tradeOrders;
	}

	/**
	 * Method setTradeOrders.
	 * @param tradeOrders List<TradeOrder>
	 */
	public void setTradeOrders(List<TradeOrder> tradeOrders) {
		this.tradeOrders = tradeOrders;
	}

	/**
	 * Method addTradeOrder.
	 * @param tradeOrders TradeOrder
	 */
	public void addTradeOrder(TradeOrder tradeOrders) {
		this.tradeOrders.add(tradeOrders);
	}

	/**
	 * Method getOpenPosition.
	 * @return TradeOrder
	 */
	@Transient
	public TradeOrder getOpenPosition() {
		for (TradeOrder order : this.getTradeOrders()) {
			if (order.getIsOpenPosition()) {
				return order;
			}
		}
		return null;
	}

	/**
	 * Method toString.
	 * @return String
	 */
	public String toString() {
		return "Trade Id: " + this.getIdTrade() + " Version: "
				+ this.getVersion() + " Avg Fill Price: "
				+ new Money(this.getAveragePrice()) + " isOpen: "
				+ this.getIsOpen() + " Open Qty: " + this.getOpenQuantity()
				+ " Total qty: " + this.getTotalQuantity() + " P/L: "
				+ new Money(this.getProfitLoss()) + " Total Value: "
				+ new Money(this.getTotalValue()) + " Total Comm: "
				+ new Money(this.getTotalCommission()) + " Side: "
				+ this.getSide();
	}
}
